package com.yytek.am.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.yytek.am.constants.AkkaConstants;
import com.yytek.am.constants.MessageType;
import com.yytek.am.core.rule.EventRuleClear;
import com.yytek.common.client.net.IotMqttClient;
import com.yytek.common.core.utils.DateUtils;
import com.yytek.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.am.mapper.FmRuleClearMapper;
import com.yytek.am.domain.FmRuleClear;
import com.yytek.am.service.IFmRuleClearService;

import javax.annotation.Resource;

/**
 * 告警清除规则定义Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-10-10
 */
@Service
public class FmRuleClearServiceImpl implements IFmRuleClearService 
{
    @Resource
    private FmRuleClearMapper fmRuleClearMapper;
    @Resource
    IotMqttClient iotMqttClient;
    /**
     * 查询告警清除规则定义
     * 
     * @param id 告警清除规则定义主键
     * @return 告警清除规则定义
     */
    @Override
    public FmRuleClear selectFmRuleClearById(Long id)
    {
        return fmRuleClearMapper.selectFmRuleClearById(id);
    }

    /**
     * 查询告警清除规则定义列表
     * 
     * @param fmRuleClear 告警清除规则定义
     * @return 告警清除规则定义
     */
    @Override
    public List<FmRuleClear> selectFmRuleClearList(FmRuleClear fmRuleClear)
    {
        return fmRuleClearMapper.selectFmRuleClearList(fmRuleClear);
    }

    /**
     * 新增告警清除规则定义
     * 
     * @param fmRuleClear 告警清除规则定义
     * @return 结果
     */
    @Override
    public int insertFmRuleClear(FmRuleClear fmRuleClear)
    {
        String userId = SecurityUtils.getUserId().toString();
        fmRuleClear.setOperator(userId);
        fmRuleClear.setCreateTime(DateUtils.getNowDate());
        fmRuleClear.setUpdateTime(DateUtils.getNowDate());
        Integer num = fmRuleClearMapper.insertFmRuleClear(fmRuleClear);
        if(num != null && num > 0){
            //发送mqtt，akka接收变更
            sendAkkaMqttRuleClearChange(AkkaConstants.EventRuleUpdateType.UPDATE,fmRuleClear);
        }
        return num;
    }

    /**
     * 发送mqtt,akka接收清除规则变更
     * @param type
     * @param fmRuleClear
     */
    public void sendAkkaMqttRuleClearChange(int type, FmRuleClear fmRuleClear) {
        EventRuleClear eventRuleClear = new EventRuleClear();
        eventRuleClear.setId(fmRuleClear.getId());
        eventRuleClear.setCreateTime(fmRuleClear.getCreateTime().getTime());
        eventRuleClear.setUpdateTime(fmRuleClear.getUpdateTime().getTime());
        eventRuleClear.setAlarmDefineId(fmRuleClear.getAlarmDefineId());
        eventRuleClear.setAccCount(fmRuleClear.getAccCount().intValue());
        eventRuleClear.setAccTime(fmRuleClear.getAccTime().intValue());
        eventRuleClear.setCalculateExpress(fmRuleClear.getCalculateExpress());
        String json = JSON.toJSONString(fmRuleClear);
        iotMqttClient.publish(MessageType.EventRule.EVENT_RULE_CLEAR.replace("+",
                String.valueOf(type)),json);
    }

    /**
     * 修改告警清除规则定义
     * 
     * @param fmRuleClear 告警清除规则定义
     * @return 结果
     */
    @Override
    public int updateFmRuleClear(FmRuleClear fmRuleClear)
    {
        String userId = SecurityUtils.getUserId().toString();
        fmRuleClear.setOperator(userId);
        fmRuleClear.setUpdateTime(DateUtils.getNowDate());
        Integer num = fmRuleClearMapper.updateFmRuleClear(fmRuleClear);

        //发送mqtt，akka接收变更
        FmRuleClear sendClear = selectFmRuleClearById(fmRuleClear.getId());
        if(sendClear != null){
            sendAkkaMqttRuleClearChange(AkkaConstants.EventRuleUpdateType.UPDATE,sendClear);
        }

        return num;
    }

    /**
     * 批量删除告警清除规则定义
     * 
     * @param ids 需要删除的告警清除规则定义主键
     * @return 结果
     */
    @Override
    public int deleteFmRuleClearByIds(Long[] ids)
    {
        if (ids.length > 0){
            for (int i = 0; i < ids.length; i++) {
                long id = ids[i];
                FmRuleClear fmRuleClear = selectFmRuleClearById(id);
                if(fmRuleClear != null){
                    //发送mqtt，akka接收变更
                    sendAkkaMqttRuleClearChange(AkkaConstants.EventRuleUpdateType.DELETE,fmRuleClear);
                }
            }
        }
        return fmRuleClearMapper.deleteFmRuleClearByIds(ids);
    }

    /**
     * 删除告警清除规则定义信息
     * 
     * @param id 告警清除规则定义主键
     * @return 结果
     */
    @Override
    public int deleteFmRuleClearById(Long id)
    {
        FmRuleClear fmRuleClear = selectFmRuleClearById(id);
        //发送mqtt，akka接收变更
        sendAkkaMqttRuleClearChange(AkkaConstants.EventRuleUpdateType.DELETE,fmRuleClear);
        return fmRuleClearMapper.deleteFmRuleClearById(id);
    }

    @Override
    public boolean execitByAlarmDefineId(Long alarmDefineId) {
        FmRuleClear cx = new FmRuleClear();
        cx.setAlarmDefineId(alarmDefineId);
        List<FmRuleClear> fmRuleClear = selectFmRuleClearList(cx);
        if(CollectionUtil.isNotEmpty(fmRuleClear)){
            return true;
        }else {
            return false;
        }
    }
}
